const Dayjs = require("dayjs");
const userExist = require("../../api/user/module/exist_user");
const cancelCode = require("./cancel");
const MYSQL = require("../../sql/mysql");
const sqlName = "verify_code";
/**
 * 核销(校验)验证码
 * @param {object} params
 * @param {string | number} params.type 验证码类型
1：支付验证码 （需要查询用户信息   需要查询创建条数）
2：手机验证码找回密码
3：绑定手机
4：验证原手机
5：绑定邮箱
6：解绑邮箱
7：邮箱找回密码
8: 手机验证码登录
9：手机验证码注册
10：邮箱验证码登录
11：邮箱验证码注册
 * @param {string} [params.user] 本次验证码仅用的用户id
 * @param {string} [params.email] 本次验证码作用的邮箱
 * @param {string | number} [params.mobile] 本次验证码作用的手机号
 * @param {string | number } params.code 验证码
 * @return {Promise<{code:number,msg:string} | {code:number,msg:string}>}
 */
module.exports = async (params) => {
  let { type, user, email, mobile, code } = params;
  type = Number(type);
  if (!type) {
    reject({ code: 203, msg: "type参数不能为空" });
    return;
  }
  if (!code) {
    reject({ code: 203, msg: "code参数不能为空" });
    return;
  }

  let querySql = `SELECT * FROM ${sqlName} WHERE `;
  let querySqlWhere = `type = '${type}' and code = '${code}'`;
  let updateCodeQueryWhere = `UPDATE ${sqlName}  SET ?`;

  if (type == 1) {
    // 支付验证码
  } else if (type == 2) {
    // 手机验证码找回密码
    if (!mobile) {
      throw { code: 500, msg: "修改失败,手机号不能为空" };
    } else if (mobile.length != 11) {
      throw { code: 500, msg: "修改失败,手机号格式不正确" };
    }
    querySqlWhere += ` and  mobile = '${mobile}' and code = '${code}' and state = '1' `;
  } else if (type == 3) {
    // 绑定手机
  } else if (type == 4) {
    // 验证原手机
  } else if (type == 5) {
    // 绑定邮箱
  } else if (type == 6) {
    // 解绑邮箱
  } else if (type == 7) {
    // 邮箱找回密码
if (!email) {
  throw { code: 500, msg: "修改失败,邮箱不能为空" };
    }
    querySqlWhere += ` and  email = '${email}' and state = '1' `;

  } else if (type == 8) {
    // 手机验证码登录
  } else if (type == 9) {
    // 9：手机验证码注册
    if (!mobile) {
      throw { code: 500, msg: "修改失败,手机号不能为空" };
    } else if (mobile.length != 11) {
      throw { code: 500, msg: "修改失败,手机号格式不正确" };
    }
    querySqlWhere += ` and  mobile = '${mobile}' and state = '1' `;
  } else if (type == 10) {
    // 邮箱验证码登录
  } else if (type == 11) {
    // 邮箱验证码注册
    if (!email) {
      throw { code: 500, msg: "修改失败,邮箱不能为空" };
    }
    querySqlWhere += ` and  email = '${email}' and code = '${code}' and state = '1' `;
  } else {
    throw { code: 500, msg: "不支持的验证码类型" };
  }


  // 查询验证码信息 ==========================
  let getCodeInfoResult = null;
  try {
    getCodeInfoResult = await MYSQL.queryPromise(querySql + querySqlWhere);
  } catch (error) {
    console.log(error);
    throw { code: 500, msg: "操作失败，在查询验证码时发生错误" };
  }
  if (!getCodeInfoResult.length) {
    console.log(querySql + querySqlWhere);
    console.log("getCodeInfoResult", getCodeInfoResult);
    throw { code: 203, msg: "验证码无效" };
  }
  // 当前时间
  let date_now = Dayjs(Date.now()).valueOf();
  // 验证码信息
  let codeInfo = getCodeInfoResult[0];
  let { id, expired_date, state } = codeInfo;
  // 作废验证码 ================================
  if (expired_date > date_now && state == 1) {
    try {
      await cancelCode({ id, state: 2 });
    } catch (error) {
      throw error;
    }
  } else {
    try {
      await cancelCode({ id, state: 0 });
    } catch (error) {
      throw { code: 203, msg: "操作失败,验证码已过期" };
    }
  }
  // 返回结果 ================================
  return {
    code: 0,
    msg: "验证成功",
  };
};
